var isEmpty = true;
var isVaild = true;

function _submit() {
    var XML = "";
    XML += "<adv>";

    var head_type = $("#head").val();
    if (head_type == "") {
        Feng.error("请设置广告头部");
        isEmpty = false;
        return isEmpty;
    }
    // if (head_type == 1) {
    //
    //
    // } else
    if (head_type == 2) {
        var picId = $("#HeadId").val();
        if (picId == "") {
            Feng.error("未填写头部图片id");
            isEmpty = false;
            return isEmpty;
        }
        var url = "";
        var ajax = new $ax(Feng.ctxPath + "/pfFile/getUrl", function (data) {
            if (data.code == 100) {
                Feng.error(data.message);
                isVaild = false;
                return isVaild;
            } else {
                url = data;
                isVaild = true;
            }
        }, function (data) {
            Feng.error("图片id有问题!" + data.responseJSON.message + "!");
        });
        ajax.set("fileId", picId.trim());
        ajax.set("type",1);
        ajax.start();
        var image = new Image();
        image.src = url;
        XML += "<head>"
        XML += "<image";
        XML += " thumb='auto:oss'";
        XML += " src='" + url + "'";
        XML += " size='" + image.width + "x" + image.height + "'";
        XML += ">";
        XML += "</image>";
        XML += "</head>"
    } else if (head_type == 3) {
        var videoId = $("#HeadId").val();
        var scTime=$("#headScTime").val();
        if (videoId == "") {
            Feng.error("未填写头部视频id");
            isEmpty = false;
            return isEmpty;
        }
        if (scTime == "") {
            Feng.error("未填写头部视频截帧秒");
            isEmpty = false;
            return isEmpty;
        }else{
            var reg = /^\d+(\.\d+)?$/;
            if (!reg.test(scTime)) {
                Feng.error("截帧秒只能为数字！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }
        }
        var str = "";
        var ajax = new $ax(Feng.ctxPath + "/pfFile/getUrl", function (data) {
            if (data.code == 100) {
                Feng.error(data.message);
                isVaild = false;
                return isVaild;
            } else {
                str = data;
                isVaild = true;
            }
        }, function (data) {
            Feng.error("视频id有问题!" + data.responseJSON.message + "!");
        });
        ajax.set("fileId", videoId.trim());
        ajax.set("type",2);
        ajax.start();
        var info=str.split(",");
        XML += "<head>"
        XML += "<video";
        XML += " thumb='" + info[0] + "?x-oss-process=video/snapshot,t_"+scTime.trim()*1000+",f_jpg'";
        XML += " src='" + info[0] + "'";
        XML += " size='" + info[1] + "x" + info[2] + "'";
        XML += " duration='" + info[3] + "'";
        XML += " length='" + info[4] + "'";
        XML += ">";
        XML += "</video>";
        XML += "</head>"
    } else if (head_type == 5) {
        var headTitle = $("#headTitle").val();
        if (headTitle == "") {
            Feng.error("未填写头部标题");
            isEmpty = false;
            return isEmpty;
        }
        XML += "<head>"
        XML += "<title>";
        XML += headTitle;
        XML += "</title>";
        XML += "</head>"
    }

    $("#content").each(function (i, e) {
        $(e).children().each(function (i, e) {
            var title = $(e).find("input[type='title']").val();
            var body = $(e).find("textarea").val();
            var picId = $(e).find("input[type='pic']").val();
            var html = $(e).find("input[type='html']").val();
            var videoId = $(e).find("input[type='video']").val();
            var scTime=$(e).find("input[type='scTime']").val();
            if (title == "") {
                Feng.error("标题不能为空！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }
            if (body == "") {
                Feng.error("正文不能为空！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }
            if (picId == "") {
                Feng.error("图片id不能为空！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }
            if (html == "") {
                Feng.error("h5不能为空！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }
            if (videoId == "") {
                Feng.error("视频id不能为空！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }
            if (scTime == "") {
                Feng.error("截帧秒不能为空！");
                isEmpty = false;
                return isEmpty;
            } else {
                isEmpty = true;
            }

            if (title != undefined && title != null) {
                XML += "<title>";
                XML += title.trim();
                XML += "</title>";
            }
            if (body != undefined && body != null) {
                XML += "<text>";
                XML += body;
                XML += "</text>";
            }
            if (html != undefined && html != null) {
                XML += "<html src='";
                XML += html;
                XML += "'></html>";
            }
            if (picId != undefined && picId != null) {
                var url = "";
                var ajax = new $ax(Feng.ctxPath + "/pfFile/getUrl", function (data) {
                    if (data.code == 100) {
                        Feng.error(data.message);
                        isVaild = false;
                        return isVaild;
                    } else {
                        url = data;
                        isVaild = true;
                    }
                }, function (data) {
                    Feng.error("图片id有问题!" + data.responseJSON.message + "!");
                });
                ajax.set("fileId", picId.trim());
                ajax.set("type",1);
                ajax.start();
                var image = new Image();
                image.src = url;
                XML += "<image";
                XML += " thumb='auto:oss'";
                XML += " src='" + url + "'";
                XML += " size='" + image.width + "x" + image.height + "'";
                XML += ">";
                XML += "</image>";
            }
            if (videoId != undefined && videoId != null) {
                var str = "";
                var ajax = new $ax(Feng.ctxPath + "/pfFile/getUrl", function (data) {
                    if (data.code == 100) {
                        Feng.error(data.message);
                        isVaild = false;
                        return isVaild;
                    } else {
                        str = data;
                        isVaild = true;
                    }
                }, function (data) {
                    Feng.error("视频id有问题!" + data.responseJSON.message + "!");
                });
                ajax.set("fileId", videoId.trim());
                ajax.set("type",2);
                ajax.start();
                var info=str.split(",");
                if (scTime != undefined && scTime != null) {
                    var reg = /^\d+(\.\d+)?$/;
                    if (!reg.test(scTime)) {
                        Feng.error("截帧秒只能为数字！");
                        isEmpty = false;
                        return isEmpty;
                    } else {
                        isEmpty = true;
                    }
                    XML += "<video";
                    XML += " thumb='" + info[0] + "?x-oss-process=video/snapshot,t_"+scTime.trim()*1000+",f_jpg'";
                    XML += " src='" + info[0] + "'";
                    XML += " size='" + info[1] + "x" + info[2] + "'";
                    XML += " duration='" + info[3] + "'";
                    XML += " length='" + info[4] + "'";
                    XML += ">";
                    XML += "</video>";
                }
            }
            // if () {
            //     var src = video.attr("src");
            //     var length = video.attr("length");
            //     var duration = video.get(0).duration;
            //     XML += "<video";
            //     XML += " thumb='" + src + "?x-oss-process=video/snapshot,t_0,f_jpg'";
            //     XML += " src='" + src + "'";
            //     XML += " size='" + video.get(0).videoWidth + "x" + video.get(0).videoHeight + "'";
            //     XML += " duration='" + duration + "'";
            //     XML += " length='" + length + "'";
            //     XML += ">";
            //     XML += "</video>";
            // }
        })

    });
    XML += "</adv>";
    localStorage.setItem('adv-content', XML);
    // self.location = "adv-review.html";
}


function _isFileImage(fileName) {
    var ext = "";
    var name = fileName.toLowerCase();
    var i = name.lastIndexOf(".");
    if (i > -1) {
        var ext = name.substring(i);
    }
    if (ext == ".png" || ext == ".jpeg" || ext == ".jpg") {
        return true
    }
    return false;
}

function _isFileVideo(fileName) {
    var ext = "";
    var name = fileName.toLowerCase();
    var i = name.lastIndexOf(".");
    if (i > -1) {
        var ext = name.substring(i);
    }
    if (ext == ".mp4") {
        return true
    }
    return false;
}

function _preview() {
    var head = $("#head").val();
    if (head == "") {
        Feng.error("请设置广告头");
        isEmpty = false;
        return isEmpty;
    } else {
        isEmpty = true;
    }
    if (head == 2) {

    }
    localStorage.removeItem('adv-content');
    _submit();
    if (!isEmpty) {
        return false;
    }
    if (!isVaild) {
        return false;
    }
    $("#head_preview").empty();
    $("#content_preview").empty();
    $("#bottom_preview").empty();
    var text = localStorage.getItem('adv-content');
    parseAndShowXML(text, 'head_preview', 'content_preview', 'bottom_preview');
    // window.open(Feng.ctxPath + "wzAdv/adv_details_preview");
}

function _showDetails() {
    var head = $("#head").val();
    if (head == "") {
        Feng.error("请设置广告头");
        isEmpty = false;
        return isEmpty;
    } else {
        isEmpty = true;
    }
    if (head == 2) {
    }
    localStorage.removeItem('adv-content');
    _submit();
    if (!isEmpty) {
        return false;
    }
    if (!isVaild) {
        return false;
    }
    var text = localStorage.getItem('adv-content');
    $("#detail").val(text);
}
